﻿using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using WaterCloud.Code;
using WaterCloud.Domain.SocialSecurityManagement;
using WaterCloud.Service;
using Microsoft.AspNetCore.Authorization;
using WaterCloud.Service.SocialSecurityManagement;
using WaterCloud.Service.SystemOrganize;
using WaterCloud.Domain.ContractManagement;


namespace WaterCloud.Web.Areas.SocialSecurityManagement.Controllers
{
    /// <summary>
    /// 创 建：超级管理员
    /// 日 期：2025-04-18 14:25
    /// 描 述：已缴纳控制器类
    /// </summary>
    [Area("SocialSecurityManagement")]
    public class InsuranceController : BaseController
    {
        public InsuranceService _service { get; set; }
        public InsuranceitemsService _itemsservice { get; set; }
        public ProvidentitemsService _pitemsservice { get; set; }
        public UserService _userservice { get; set; }
        public OrganizeService _orgservice { get; set; }
        public BigsickitemsService _bigsickitemsservice { get; set; }

        #region 获取数据
        //未缴纳
        [HttpGet]
        public virtual ActionResult Index1()
        {
            return View();
        }
        //未取消
        [HttpGet]
        public virtual ActionResult Index2()
        {
            return View();
        }
        //已取消
        [HttpGet]
        public virtual ActionResult Index3()
        {
            return View();
        }
        [HandlerAjaxOnly]
        [IgnoreAntiforgeryToken]
        public async Task<ActionResult> GetGridJson(SoulPage<InsuranceEntity> pagination, string keyword)
        {
            if (string.IsNullOrEmpty(pagination.field))
            {
                pagination.field = "F_Id";
                pagination.order = "desc";
            }
            var data = await _service.GetLookList(pagination, keyword);
            if (data.Count > 0) {
                for (int i = 0; i < data.Count; i++)
                {
                    if (!string.IsNullOrEmpty(data[i].F_InsuranceItems))
                    {
                        List<string> str = new List<string>();

                        foreach (var item in data[i].F_InsuranceItems.Split(','))
                        {
                            var temp = await _itemsservice.GetForm1(item);
                            if (temp != null)
                            {
                                str.Add(temp.F_FullName);

                            }
                        }
                        data[i].F_InsuranceItemsName = string.Join("  ", str.ToArray());

                    }
                    if (!string.IsNullOrEmpty(data[i].F_ProvidentItems))
                    {
                        List<string> str = new List<string>();

                        foreach (var item in data[i].F_ProvidentItems.Split(','))
                        {
                            var temp = await _pitemsservice.GetForm1(item);
                            if (temp != null)
                            {
                                str.Add(temp.F_FullName);

                            }
                        }
                        data[i].F_ProvidentItemsName = string.Join("  ", str.ToArray());

                    }

                    data[i].F_Amountsick = _bigsickitemsservice.GetAmountsick();
                }

            }
            return Content(pagination.setData(data).ToJson());
        }
        [HandlerAjaxOnly]
        [IgnoreAntiforgeryToken]
        public async Task<ActionResult> GetGridJson2(SoulPage<InsuranceEntity> pagination, string keyword)
        {
            if (string.IsNullOrEmpty(pagination.field))
            {
                pagination.field = "F_Id";
                pagination.order = "desc";
            }
            var data = await _service.GetGridJson2(pagination, keyword);
            if (data.Count > 0)
            {
                for (int i = 0; i < data.Count; i++)
                {
                    if (!string.IsNullOrEmpty(data[i].F_InsuranceItems))
                    {
                        List<string> str = new List<string>();

                        foreach (var item in data[i].F_InsuranceItems.Split(','))
                        {
                            var temp = await _itemsservice.GetForm1(item);
                            if (temp != null)
                            {
                                str.Add(temp.F_FullName);

                            }
                        }
                        data[i].F_InsuranceItemsName = string.Join("  ", str.ToArray());

                    }
                    if (!string.IsNullOrEmpty(data[i].F_ProvidentItems))
                    {
                        List<string> str = new List<string>();

                        foreach (var item in data[i].F_ProvidentItems.Split(','))
                        {
                            var temp = await _pitemsservice.GetForm1(item);
                            if (temp != null)
                            {
                                str.Add(temp.F_FullName);

                            }
                        }
                        data[i].F_ProvidentItemsName = string.Join("  ", str.ToArray());

                    }
                    data[i].F_Amountsick = _bigsickitemsservice.GetAmountsick();
                }
            }
            return Content(pagination.setData(data).ToJson());
        }

        [HandlerAjaxOnly]
        [IgnoreAntiforgeryToken]
        public async Task<ActionResult> GetGridJson3(SoulPage<InsuranceEntity> pagination, string keyword)
        {
            if (string.IsNullOrEmpty(pagination.field))
            {
                pagination.field = "F_Id";
                pagination.order = "desc";
            }
            var data = await _service.GetGridJson3(pagination, keyword);
            if (data.Count > 0)
            {
                for (int i = 0; i < data.Count; i++)
                {
                    if (!string.IsNullOrEmpty(data[i].F_InsuranceItems))
                    {
                        List<string> str = new List<string>();

                        foreach (var item in data[i].F_InsuranceItems.Split(','))
                        {
                            var temp = await _itemsservice.GetForm1(item);
                            if (temp != null)
                            {
                                str.Add(temp.F_FullName);

                            }
                        }
                        data[i].F_InsuranceItemsName = string.Join("  ", str.ToArray());

                    }
                    if (!string.IsNullOrEmpty(data[i].F_ProvidentItems))
                    {
                        List<string> str = new List<string>();

                        foreach (var item in data[i].F_ProvidentItems.Split(','))
                        {
                            var temp = await _pitemsservice.GetForm1(item);
                            if (temp != null)
                            {
                                str.Add(temp.F_FullName);

                            }
                        }
                        data[i].F_ProvidentItemsName = string.Join("  ", str.ToArray());

                    }
                    data[i].F_Amountsick = _bigsickitemsservice.GetAmountsick();
                }

            }

            return Content(pagination.setData(data).ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public async Task<ActionResult> GetFormJson(string keyValue)
        {
            var data = await _service.GetLookForm(keyValue);
            if (!string.IsNullOrEmpty(data.F_InsuranceItems))
            {
                List<string> str = new List<string>();

                foreach (var item in data.F_InsuranceItems.Split(','))
                {
                    var temp = await _itemsservice.GetForm1(item);
                    if (temp != null)
                    {
                        str.Add(temp.F_FullName);

                    }
                }
                data.F_InsuranceItemsName = string.Join("  ", str.ToArray());
            }
            if (!string.IsNullOrEmpty(data.F_ProvidentItems))
            {
                List<string> str = new List<string>();

                foreach (var item in data.F_ProvidentItems.Split(','))
                {
                    var temp = await _pitemsservice.GetForm1(item);
                    if (temp != null)
                    {
                        str.Add(temp.F_FullName);

                    }
                }
                data.F_ProvidentItemsName = string.Join("  ", str.ToArray());

            }

            data.F_Amountsick = _bigsickitemsservice.GetAmountsick();
            return Content(data.ToJson());
        }

        public async Task<ActionResult> GetListJson(string keyword, string ids)
        {
            var data = await _service.GetList(keyword);
            data = data.ToList();
            if (!string.IsNullOrEmpty(ids))
            {
                foreach (var item in ids.Split(','))
                {
                    var temp = data.Find(a => a.F_Id == item);
                    if (temp != null)
                    {
                        temp.LAY_CHECKED = true;
                    }
                }


                for (int i = 0; i < data.Count; i++)
                {
                    if (!string.IsNullOrEmpty(data[i].F_InsuranceItems))
                    {
                        List<string> str = new List<string>();

                        foreach (var item in data[i].F_InsuranceItems.Split(','))
                        {
                            var temp = await _itemsservice.GetForm1(item);
                            if (temp != null)
                            {
                                str.Add(temp.F_FullName);

                            }
                        }
                        data[i].F_InsuranceItemsName = string.Join("  ", str.ToArray());

                    }
                    if (!string.IsNullOrEmpty(data[i].F_ProvidentItems))
                    {
                        List<string> str = new List<string>();

                        foreach (var item in data[i].F_ProvidentItems.Split(','))
                        {
                            var temp = await _pitemsservice.GetForm1(item);
                            if (temp != null)
                            {
                                str.Add(temp.F_FullName);

                            }
                        }
                        data[i].F_ProvidentItemsName = string.Join("  ", str.ToArray());

                    }

                    data[i].F_Amountsick = _bigsickitemsservice.GetAmountsick();
                }
            }
            return Success(data.Count, data);
        }
        #endregion

        #region 提交数据
        [HttpPost]
        [HandlerAjaxOnly]
        public async Task<ActionResult> SubmitForm(InsuranceEntity entity, string keyValue)
        {
            try
            {
                await _service.SubmitForm(entity, keyValue);
                return await Success("操作成功。", "", keyValue);
            }
            catch (Exception ex)
            {
                return await Error(ex.Message, "", keyValue);
            }
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize]
        public async Task<ActionResult> DeleteForm(string keyValue)
        {
            try
            {
                await _service.DeleteForm(keyValue);
                return await Success("操作成功。", "", keyValue, DbLogType.Delete);
            }
            catch (Exception ex)
            {
                return await Error(ex.Message, "", keyValue, DbLogType.Delete);
            }
        }
        #endregion
    }
}
